#!/bin/bash
clear

TEXT='\033[30;107;2m'
INFO='\033[97;44m'
WARNING='\033[97;43m'
ERROR='\033[97;41m'
SUCCESS='\033[97;42m'
GREEN='\033[92m'
YELLOW='\033[93m'
RESET='\033[0m'

if [ "${EUID}" -ne 0 ]; then
  echo -e "${ERROR} RALAT ${RESET}${TEXT} Anda tiada kebenaran untuk menjalankan skrip ini! ${RESET}"
  exit 1
fi

function installPkg {
  read -p "Teruskan dengan pemasangan semula? [Y/n] " _yesno
  if [[ "$_yesno" =~ ^[Yy]$ ]]; then
    if [[ ! -d /etc/squid ]]; then
      wget -q -O - https://raw.githubusercontent.com/cybertize/debian/buster/packages/squid.sh | bash
    fi
    apt-get -y -qq --purge remove squid &>/dev/null
    apt-get -y -qq autoremove &>/dev/null
    apt-get -qq clean &>/dev/null
    clear
    sleep 3
    wget -q -O - https://raw.githubusercontent.com/cybertize/debian/buster/packages/squid.sh | bash
  else
    echo
    echo -e "${ERROR} INFO ${RESET}${TEXT} Membatalkan pemasangan semula ${RESET}"
    exit 1
    echo
  fi
}

function uninstallPkg {
  read -r -p "Adakah anda pasti mahu menyahpasang squid? [Y/n] " __yesno
  if [[ $_yesno = ^[Yy]$ ]]; then
    {
      apt-get -y -qq --purge remove squid
      apt-get -qq autoremove
      apt-get -qq autoclean
    } &>/dev/null

    echo -e "${SUCCESS} INFO ${RESET}${TEXT} Bersihkan & Keluarkan pakej squid ${RESET}"
    exit 0
  else
    echo -e "${ERROR} INFO ${RESET}${TEXT} Batalkan penyahpasangan pakej ${RESET}"
    exit 1
  fi
}

function configurePkg {
  squid_ports=($(cat /etc/squid/squid.conf | grep -w 'http_port' | cut -d ' ' -f 2,4,6))

  echo
  echo -e "Secara lalai perkhidmatan squid menggunakan 3 port $squid_ports"
  echo
  while true; do
    read -r -p "Adakah anda mahu ganti port baharu? [Y/n] " __yesno
    if [[ "$__yesno" =~ ^[Yy]$ ]]; then
      read -p "Masukkan port baru: " first_port second_port third_port
      if [[ -n "$first_port" && "$first_port" =~ ^[0-9]+$ ]]; then
        sed -i 's|${squid_ports[0]}|$first_port|g' /etc/squid/squid.conf
      fi
      if [[ -n "$second_port" && "$second_port" =~ ^[0-9]+$ ]]; then
        sed -i 's|${squid_ports[1]}|$second_port|g' /etc/squid/squid.conf
      fi
      if [[ -n "$third_port" && "$third_port" =~ ^[0-9]+$ ]]; then
        sed -i 's|${squid_ports[2]}|$third_port|g' /etc/squid/squid.conf
      fi

      echo -e "${INFO} INFO ${RESET}${TEXT} Perubahan telah dibuat ${RESET}"
      break
    else
      echo -e "${INFO} INFO ${RESET}${TEXT} Tiada perubahan dibuat ${RESET}"
      break
    fi
  done
}

function detailPkg {
  local unit_id=$(systemctl show squid.service -p Id | cut -d '=' -f 2)
  local unit_name=$(systemctl show squid.service -p Names | cut -d '=' -f 2)
  local unit_desc=$(systemctl show squid.service -p Description | cut -d '=' -f 2)
  local is_active=$(systemctl is-active squid.service)
  local is_enable=$(systemctl is-enabled squid.service)
  local squid_ports=$(cat /etc/squid/squid.conf | grep -w 'http_port' | cut -d ' ' -f 2,4,6 | tr '\n' ' ')

  echo
  echo -e "${TEXT} ========================================================== ${RESET}"
  echo -e "${TEXT} Butiran perkhidmatan Squid                                 ${RESET}"
  echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
  echo
  echo -e "${YELLOW}     ID:${RESET} ${GREEN}$unit_id${RESET}"
  echo -e "${YELLOW}   Name:${RESET} ${GREEN}$unit_name${RESET}"
  echo -e "${YELLOW}   Desc:${RESET} ${GREEN}$unit_desc${RESET}"
  echo -e "${YELLOW} Status:${RESET} ${GREEN}$is_active & $is_enable${RESET}"
  echo -e "${YELLOW}  Ports:${RESET} ${GREEN}$squid_ports${RESET}"
  echo
  echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
  echo -e "${TEXT} Dicipta oleh Doctype, Dikuasakan oleh Cybertize.           ${RESET}"
  echo -e "${TEXT} ========================================================== ${RESET}"
  echo
}

function squidMenu {
  echo
  echo -e "${TEXT} ========================================================== ${RESET}"
  echo -e "${TEXT} MENU SQUID                                                 ${RESET}"
  echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
  echo
  echo -e "[01] ${GREEN}reinstall${RESET}  - ${YELLOW}Reinstall squid package${RESET}"
  echo -e "[02] ${GREEN}uninstall${RESET}  - ${YELLOW}Uninstall squid package${RESET}"
  echo -e "[03] ${GREEN}configure${RESET}  - ${YELLOW}Edit/Configure squid config${RESET}"
  echo -e "[04] ${GREEN}detail   ${RESET}  - ${YELLOW}Show squid detail & status${RESET}"
  echo -e "[00] ${GREEN}quit     ${RESET}  - ${YELLOW}Exit from menu${RESET}"
  echo
  echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
  echo -e "${TEXT} Dicipta oleh Doctype, Dikuasakan oleh Cybertize.           ${RESET}"
  echo -e "${TEXT} ========================================================== ${RESET}"
  echo
  while true; do
    read -p "Masukkan pilihan anda: " _choice
    case "$_choice" in
    01 | reinstall)
      installPkg && break
      ;;
    02 | uninstall)
      uninstallPkg && break
      ;;
    03 | configure)
      configurePkg && break
      ;;
    04 | detail)
      detailPkg && break
      ;;
    00 | quit)
      exit 0
      ;;
    *)
      echo -e "${ERROR} RALAT ${RESET}${TEXT} Pilihan tidak sah ${RESET}"
      squidMenu
      ;;
    esac
  done
}

if [[ -f /etc/squid/squid.conf ]]; then
  squidMenu
elif [[ ! -f /etc/squid/squid.conf ]]; then
  installPkg
fi
